package org.jxc.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.SalesBackOrderProductDAO;
import org.jxc.domain.SalesBackOrderProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;

@Service
public class SalesBackOrderProductService implements IService {

	private SalesBackOrderProductDAO salesBackOrderProductDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		salesBackOrderProductDAO = (SalesBackOrderProductDAO) DAOFactory
				.getDAO(sessionFactory, SalesBackOrderProductDAO.class);
	}

	/**
	 * 根据订单ID获取隶属于其的销售订单明细
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<SalesBackOrderProduct> findAllSalesBackOrderProductBySalesBackOrderId(
			String salesBackOrderId) throws CRUDException {
		List<SalesBackOrderProduct> list = salesBackOrderProductDAO.find(
				"from SalesBackOrderProduct where salesBackOrder.id = ?",
				salesBackOrderId);
		return list;
	}

	/**
	 * 根据id查询退货单明细
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public SalesBackOrderProduct findById(String id) throws Exception {
		SalesBackOrderProduct salesBackOrderProduct = salesBackOrderProductDAO
				.get(id);
		return salesBackOrderProduct;
	}

	/**
	 * 新建采购订单明细
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void saveSalesBackOrderProduct(
			SalesBackOrderProduct salesBackOrderProduct) throws CRUDException {
		salesBackOrderProductDAO.save(salesBackOrderProduct);
	}

	/**
	 * 修改采购订单明细
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void updateSalesBackOrderProduct(
			SalesBackOrderProduct salesBackOrderProduct) throws CRUDException {
		SalesBackOrderProduct old = salesBackOrderProductDAO
				.get(salesBackOrderProduct.getId());

		old.setAmounts(salesBackOrderProduct.getAmounts());
		salesBackOrderProductDAO.update(old);
	}

	public void deleteSalesBackOrderProductBySalesOrderId(
			String salesBackOrderOrderId) throws CRUDException {
		List<SalesBackOrderProduct> list = findAllSalesBackOrderProductBySalesBackOrderId(salesBackOrderOrderId);
		for (SalesBackOrderProduct sbop : list) {
			salesBackOrderProductDAO.delete(sbop);
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteSalesBackOrderProduct(List<String> idList)
			throws CRUDException {
		salesBackOrderProductDAO.deleteByIdList(idList);
	}

}